package utils;

import analyser.Analyser;
import entity.Message;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Adiy on 2017-03-15.
 */
public class TFcount {
    Analyser analyser;
    public TFcount(){
        init();
    }
    //初始化分词器
    public void init(){
        analyser = new Analyser();
    }
    public List<String> splitMessage(Message message) {
        String content = message.getContent();
        List<String> words = analyser.analyse(content);
        return words;
    }
    public List<Map<String,Integer>> countIF(List<Message> msgList){
        List<Map<String,Integer>> list=new ArrayList<Map<String, Integer>>();
        for(int i=0;i<msgList.size();i++){
            Message msg=msgList.get(i);
            List<String> words=splitMessage(msg);
            System.out.println(words);
            Map<String,Integer> map=new HashMap<String,Integer>();
            for(int j=0;j<words.size();j++){
                if(map.containsKey(words.get(j))){
                    int value=map.get(words.get(j));
                    value++;
                    map.put(words.get(j),value);
                }else{
                    map.put(words.get(j),1);
                }

            }
            list.add(map);
            System.out.println(map);
        }
        return list;
    }

    public static void main(String[] args){

        TFcount tfC=new TFcount();

        Message message=new Message();
        message.setContent("我爱北京天安门，我爱大自然,我爱万里长城");

        List<String> words1=tfC.splitMessage(message);
        Map<String,Integer> map1=new HashMap<String,Integer>();
        for(int j=0;j<words1.size();j++){
            if(map1.containsKey(words1.get(j))){
                int value=map1.get(words1.get(j));
                value++;
                map1.put(words1.get(j),value);
            }else{
                map1.put(words1.get(j),1);
            }

        }
        System.out.println(map1);




    }

}
